package com.wbs.shop.nuby.view.base;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.LayoutRes;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import com.wbs.shop.nuby.utils.Logger;
import com.wbs.shop.nuby.view.listener.RecoveryListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.subjects.BehaviorSubject;

/**
 * Created by Administrator on 2016/12/3.
 */
public class BaseActivity extends FragmentActivity implements LifecycleProvider {
    protected final BehaviorSubject<LifecycleEvent> lifeSubject = BehaviorSubject.create();
    private Unbinder bind;
    private CompositeDisposable mCompositeDisposable;
    private List<BaseViewModel> viewModels = new ArrayList<>();
    private List<RecoveryListener> recoveryListeners = new ArrayList<>();
    protected BroadcastReceiver mBroadcastReceiver;


    public void addViewModel(BaseViewModel viewModel) {
        if (viewModel != null)
            viewModels.add(viewModel);
    }

    public void addRecoveryListener(RecoveryListener recoveryListener) {
        if (recoveryListener != null)
            recoveryListeners.add(recoveryListener);
    }

    public BehaviorSubject<LifecycleEvent> getLifeSubject() {
        return lifeSubject;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        lifeSubject.onNext(LifecycleEvent.CREATE);
        mCompositeDisposable = new CompositeDisposable();
        if (mBroadcastReceiver != null) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
            mBroadcastReceiver = null;
        }
    }

    /**
     * 用户登录，一般情况是被挤下线才重新登录才会调用
     */
    public void userLogin() {
    }

    public void add(Disposable d) {
        if (mCompositeDisposable != null)
            mCompositeDisposable.add(d);
    }

    @Override
    protected void onStart() {
        super.onStart();
        lifeSubject.onNext(LifecycleEvent.START);
    }

    @Override
    public void setContentView(@LayoutRes int layoutResID) {
        try {
            super.setContentView(layoutResID);
            bind = ButterKnife.bind(this);
        }catch (Exception e){
            Logger.e(e);
        }
        // Utils.setDefaultStatusBarFont(this);
    }

    public boolean hasExtras() {
        return getIntent() != null && getIntent().getExtras() != null;
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (viewModels != null && !viewModels.isEmpty()) {
            for (BaseViewModel viewModel : viewModels) {
                viewModel.onResume();
            }
        }
        lifeSubject.onNext(LifecycleEvent.RESUME);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (viewModels != null && !viewModels.isEmpty()) {
            for (BaseViewModel viewModel : viewModels) {
                viewModel.onPause();
            }
        }
        lifeSubject.onNext(LifecycleEvent.PAUSE);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (viewModels != null && !viewModels.isEmpty()) {
            for (BaseViewModel viewModel : viewModels) {
                viewModel.onSaveInstanceState(outState);
            }
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        if (viewModels != null && !viewModels.isEmpty()) {
            for (BaseViewModel viewModel : viewModels) {
                viewModel.onStop();
            }
        }
        lifeSubject.onNext(LifecycleEvent.STOP);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        try {
            lifeSubject.onNext(LifecycleEvent.DESTROY);
            if (viewModels != null && !viewModels.isEmpty()) {
                for (BaseViewModel viewModel : viewModels) {
                    viewModel.onDestroy();
                }
                viewModels.clear();
            }
            if (recoveryListeners != null && !recoveryListeners.isEmpty()) {
                for (RecoveryListener recoveryListener : recoveryListeners) {
                    recoveryListener.onDestroy();
                }
                recoveryListeners.clear();
            }
            if (bind != null) {
                bind.unbind();
            }
            if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
                mCompositeDisposable.dispose();
            }
            if (mBroadcastReceiver != null)
                LocalBroadcastManager.getInstance(this).unregisterReceiver(mBroadcastReceiver);
        } catch (Exception e) {
            Logger.e(e);
        }
    }

    final public void goActicity(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
    }

    final public void goActicity(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    final public void goActicityFinish(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
        finish();
    }

    final public void goActicityFinish(Class<?> clazz) {
        Intent intent = new Intent(this, clazz);
        startActivity(intent);
        finish();
    }

    final public void goActicityForResult(Class<?> clazz, int requestCode) {
        Intent intent = new Intent(this, clazz);
        startActivityForResult(intent, requestCode);
    }


    final public void goActicityForResult(Class<?> clazz, int requestCode, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, requestCode);
    }
}
